import Opt from '@/components/dt/info/Opt.js'
import toolMgr from '@/biz/base/tool.js'
import ruleOpt from '@/biz/option/rule.js'
import uploadOpt from '@/biz/option/upload.js'
class ContractOpt extends Opt {

  constructor(info, prev, next, isCreate) {
    super(info)
    this.prev = prev // 上一条数据
    this.next = next // 下一条数据
    this.isCreate = isCreate
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let endDate = {
      key: 'contractEndTime',
      placeholder: '请选择合同结束日期',
      col: 12,
      visible: this.info.endType == '1',
      date: {
        disabled: (date) => {
          if (this.next) {
            return date.valueOf() > new Date(this.next.contractBeginTime).getTime() -
              86400000 ||
              date.valueOf() < new Date(this.info.contractBeginTime).getTime() - 86400000
          } else {
            return date.valueOf() < new Date(this.info.contractBeginTime).getTime() -
              86400000
          }
        },
      }
    }
    let endInput = {
      key: 'contractEndTime',
      placeholder: '请输入合同结束说明',
      col: 12,
      visible: this.info.endType == '2',
      input: {}
    }
    let endDateRules = {
      rules: [
        ruleOpt.required(this.info.endType == '1' ? '请选择合同结束日期' : '请输入合同结束说明'),
      ]
    }

    // let files = []
    // if (this.info.attachList && this.info.attachList.length > 0) {
    //   console.log(this.info.attachList,'this.info.attachListthis.info.attachList');
    //   this.info.attachList.forEach(item => {
    //     if (item) {
    //       files.push({
    //         name: item.name,
    //         url: item.url
    //       })
    //     }
    //   })
    // }
    // let accessory = {
    //   title: '合同附件',
    //   key: 'attachList',
    //   visible: this.info.isSign,
    //   upload: {
    //     maxSize: 51200,
    //     title: '附件上传',
    //     api: toolMgr.unloadFile(),
    //     files,
    //     multiple: true,
    //   },
    //   data: {
    //     to: (v) => {
    //       let attachList = v.map(item => {
    //         return {
    //           imageName: item.name,
    //           imageURL: item.response ? item.response.data.imageURL : item.url
    //         }
    //       })
    //       return {
    //         attachList
    //       }
    //     }
    //   },
    //   rules: [
    //     ruleOpt.required('请上传附件', 'array')
    //   ]
    // }

    this.all = [{
        title: '合同名称',
        placeholder: '请输入合同名称',
        key: 'contractName',
        col: 18,
        input: {},
        rules: [
          ruleOpt.required('请输入合同名称'),
          ruleOpt.max(50)
        ]
      },
      {
        title: '合同开始日期',
        placeholder: '请选择合同开始日期',
        key: 'contractBeginTime',
        col: 18,
        disabled: this.isCreate && this.prev && this.prev.endType == '1' ? true : false,
        date: {
          disabled: (date) => {
            if (this.prev) {
              if (this.prev.endType == '1') {
                return date.valueOf() < new Date(this.prev.contractEndTime).getTime()
              } else {
                return date.valueOf() < new Date(this.prev.contractBeginTime).getTime()
              }
            } else {
              return false
            }
          },
        },
        rules: [
          ruleOpt.required('请选择合同开始日期')
        ]
      },
      {
        title: '合同结束日期',
        key: 'contractEndTime',
        tip: '若合同结束非具体日期，请将“日期”调整为“约定条件”，并输入具体合同结束条件',
        group: [
          {
            key: 'endType',
            col: 5,
            select: {
              options: [
                { key: '1', value: '日期' },
                { key: '2', value: '约定条件' },
              ]
            },
            change: ({ info }) => {
              this.info.contractEndTime = null
              endDateRules.rules.slice(0, 1)
              endDateRules.rules[0] = ruleOpt.required(info.endType == '1' ?
                '请选择合同结束日期' : '请输入合同结束说明')
              if (info.endType == '1') {
                endDate.visible = true
                endInput.visible = false
              } else {
                endDate.visible = false
                endInput.visible = true
                endDateRules.rules.push(ruleOpt.max(50))
              }
            },
          },
          endDate,
          endInput
        ],
        ...endDateRules
      },
      // accessory
      uploadOpt.fileOss({
        title: '代理合同附件',
        placeholder: '(最大支持50M文件，若文件过大请压缩或分多个文件)',
        key: 'attachList',
        maxlength: 10,
        hideDel: false,
        show: false,
        maxSize: 51200,
        showDel:true,
        multiple: true,
        // itemName: 'imageName',
        // itemUrl: 'imageURL',
        rules: [
          ruleOpt.required('请选择上传代理合同', 'array')
        ]
      }, this.info),
    ]
  }
}

export default ContractOpt